#include <iostream>
#include <fstream>
#include <stdexcept>
#include <cmath> 
#include <vector>
#include "Function.hpp"
#include "Interpolation.hpp"
#define PI 3.14159265358979323846


class F1 : public Function {
public:
    double operator() (double x) const {
        return 1/(1 + 25*x*x);
    }
};

std::vector<double> polynomial(double x, int n) {
    std::vector<double> xVec;
    std::vector<double> yVec;
    F1 f1;
    for(int k = 1; k <= n;k++ ){
        xVec.push_back(cos((2 * k - 1) * PI / (2 * n)));
        yVec.push_back(f1(xVec[k - 1]));
    }
    NewtonInterpolation interpolation(xVec, yVec);
    return interpolation.interpolate(x);  
}
int main() {
    for (int n = 5; n <= 20; n = n+5) {
        std::ofstream file("data_n" + std::to_string(n) + ".csv");
        if (!file.is_open()) {
            std::cerr << "Failed to open file" << std::endl;
            return 1;
        }

        for (double x = -1; x <= 1; x += 0.01) {
            std::vector<double> y = polynomial(x, n);
            file << x << "," << y[0] << std::endl;
        }

        file.close();
    }
    return 0; 
}
